﻿/* ****************************************************************************
 *
 * Copyright (c) Microsoft Corporation. 
 *
 * This source code is subject to terms and conditions of the Microsoft Public License. A
 * copy of the license can be found at http://go.microsoft.com/fwlink/?LinkID=131993. If
 * you cannot locate the Microsoft Public License, please send an email to
 * mixon@microsoft.com. By using this source code in any fashion, you are agreeing to
 * be bound by the terms of the Microsoft Public License.
 *
 * You must not remove this notice, or any other, from this software.
 *
 *
 * ***************************************************************************/

using System;
using System.Windows.Controls;

namespace MIXOnline.Descry
{

    /// <summary>
    /// Class that appears at the top of the timeline to indicate time and time intervals visually
    /// </summary>
    public partial class ScalePanel : Grid
    {
        public ScalePanel()
        {
            InitializeComponent();
            InitializeGrids();
        }

        #region Properties

        private DateTime _StartingDateTime;
        public DateTime StartingDateTime
        {
            get { return _StartingDateTime; }
            set
            {
                _StartingDateTime = value;
                textBlockStartDateTime.Text = _StartingDateTime.ToString("MMM d yyyy"); // update time stamp displayed
                if (_view != null) DrawScale(ScaleGrid, _view);
            }
        }

        public View View
        {
            get { return _view; }
            set
            {
                _view = value;
                DrawScale(ScaleGrid, _view);
            }
        }

        #endregion Properties

        #region Event Handlers

        public event ViewEventHandler ViewChanged;

        public delegate void ViewEventHandler(object sender, ViewEventArgs fe);

        protected virtual void OnViewChanged(ViewEventArgs ve)
        {
            ViewEventHandler handler = this.ViewChanged;
            if (handler != null)
            {
                handler(this, ve);
            }
        }

        #endregion Event Handlers

        #region Methods

        private void InitializeGrids()
        {
            DrawScale(ScaleGrid, _newView);
        }

        private void DrawScale(Grid grid, View view)
        {
            grid.Children.Clear();
            grid.ColumnDefinitions.Clear();

            switch (view.Unit)
            {
                case Unit.FifteenMinute:
                    for (int i = 0; i < view.Intervals; i++) // divide 15 min into 15 intervals
                    {
                        grid.ColumnDefinitions.Add(new ColumnDefinition());
                        ScaleMarker sm = new ScaleMarker(null, i.ToString());
                        grid.Children.Add(sm);
                        Grid.SetColumn(sm, i);
                    }
                    break;

                case Unit.Hour:
                    long intervalTicks = new TimeSpan(view.TimeSpan.Ticks / view.Intervals).Ticks;
                    for (int i = 0; i < view.Intervals; i++) // divide hour into 5 minute intervals
                    {
                        grid.ColumnDefinitions.Add(new ColumnDefinition() );

                        DateTime time = StartingDateTime + new TimeSpan(intervalTicks * i);
                        ScaleMarker sm = new ScaleMarker(null, time.ToString("h:mm tt"));

                        grid.Children.Add(sm);
                        Grid.SetColumn(sm, i);
                    }
                    break;

                case Unit.Day:
                    for (int i = 0; i < view.Intervals; i++) // divide into 24 hours
                    {
                        grid.ColumnDefinitions.Add(new ColumnDefinition());
                        DateTime hour = new DateTime(2008, 1, 1) + new TimeSpan(0, i*2, 0, 0);
                        ScaleMarker sm = new ScaleMarker(null, hour.ToString("h tt"));
                        grid.Children.Add(sm);
                        Grid.SetColumn(sm, i);
                    }
                    break;

                //case Unit.HalfDay:
                //    for (int i = 0; i < view.Intervals; i++) // divide into 12 hours
                //    {
                //        grid.ColumnDefinitions.Add(new ColumnDefinition());
                //        ScaleMarker sm = new ScaleMarker(null, i.ToString());
                //        grid.Children.Add(sm);
                //        Grid.SetColumn(sm, i);
                //    }
                //    break;

                case Unit.Year:
                    for (int i = 0; i < view.Intervals; i++) // divide into 12 months
                    {
                        grid.ColumnDefinitions.Add(new ColumnDefinition());
                        // pretty print month names
                        ScaleMarker sm = new ScaleMarker(null, new DateTime(2008, i + 1, 1).ToString("MMMMMMMMMMMM"));
                        grid.Children.Add(sm);
                        Grid.SetColumn(sm, i);
                    }
                    break;

                case Unit.Week:
                    for (int i = 0; i < view.Intervals; i++) // divide into 7 days
                    {
                        grid.ColumnDefinitions.Add(new ColumnDefinition());
                        // pick a random Sunday and add days to it for pretty print
                        DateTime day = new DateTime(2008, 10, 26) + new TimeSpan(i, 0, 0, 0);
                        ScaleMarker sm = new ScaleMarker(null, day.ToString("ddddddd"));
                        grid.Children.Add(sm);
                        Grid.SetColumn(sm, i);
                    }
                    break;

                // TODO: Make sure month has right # of days. For now we go with 31 days
                case Unit.Month:
                    for (int i = 0; i < view.Intervals; i++) // divide into 31 days
                    {
                        ScaleMarker sm = new ScaleMarker(null, (i + 1).ToString());
                        AddColumnDefinition(grid, sm, i);
                    }
                    break;

            }
        }

        private void AddColumnDefinition(Grid grid, ScaleMarker scalemarker, int columnindex)
        {
            grid.ColumnDefinitions.Add(new ColumnDefinition());
            grid.Children.Add(scalemarker);
            Grid.SetColumn(scalemarker, columnindex);
        }

        #endregion Methods

        #region Privates

        private View _view;
        private View _newView = new Views().GetView("Hour");
        //private bool dragging = false;
        //private Point X = new Point();

        #endregion Privates
    }
}
